using System;

namespace HslCommunication.Core
{
	/// <summary>
	/// 字节倒序的转换类<br />
	/// Byte reverse order conversion class
	/// </summary>
	public class ReverseBytesTransform : ByteTransformBase
	{
		/// <inheritdoc cref="M:HslCommunication.Core.ByteTransformBase.#ctor" />
		public ReverseBytesTransform()
		{
		}

		/// <inheritdoc cref="M:HslCommunication.Core.ByteTransformBase.#ctor(HslCommunication.Core.DataFormat)" />
		public ReverseBytesTransform(DataFormat dataFormat)
			: base(dataFormat)
		{
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransInt16(System.Byte[],System.Int32)" />
		public override short TransInt16(byte[] buffer, int index)
		{
			return BitConverter.ToInt16(new byte[2]
			{
				buffer[1 + index],
				buffer[index]
			}, 0);
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransUInt16(System.Byte[],System.Int32)" />
		public override ushort TransUInt16(byte[] buffer, int index)
		{
			return BitConverter.ToUInt16(new byte[2]
			{
				buffer[1 + index],
				buffer[index]
			}, 0);
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransInt32(System.Byte[],System.Int32)" />
		public override int TransInt32(byte[] buffer, int index)
		{
			return BitConverter.ToInt32(ByteTransDataFormat4(new byte[4]
			{
				buffer[3 + index],
				buffer[2 + index],
				buffer[1 + index],
				buffer[index]
			}), 0);
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransUInt32(System.Byte[],System.Int32)" />
		public override uint TransUInt32(byte[] buffer, int index)
		{
			return BitConverter.ToUInt32(ByteTransDataFormat4(new byte[4]
			{
				buffer[3 + index],
				buffer[2 + index],
				buffer[1 + index],
				buffer[index]
			}), 0);
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransInt64(System.Byte[],System.Int32)" />
		public override long TransInt64(byte[] buffer, int index)
		{
			return BitConverter.ToInt64(ByteTransDataFormat8(new byte[8]
			{
				buffer[7 + index],
				buffer[6 + index],
				buffer[5 + index],
				buffer[4 + index],
				buffer[3 + index],
				buffer[2 + index],
				buffer[1 + index],
				buffer[index]
			}), 0);
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransUInt64(System.Byte[],System.Int32)" />
		public override ulong TransUInt64(byte[] buffer, int index)
		{
			return BitConverter.ToUInt64(ByteTransDataFormat8(new byte[8]
			{
				buffer[7 + index],
				buffer[6 + index],
				buffer[5 + index],
				buffer[4 + index],
				buffer[3 + index],
				buffer[2 + index],
				buffer[1 + index],
				buffer[index]
			}), 0);
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransSingle(System.Byte[],System.Int32)" />
		public override float TransSingle(byte[] buffer, int index)
		{
			return BitConverter.ToSingle(ByteTransDataFormat4(new byte[4]
			{
				buffer[3 + index],
				buffer[2 + index],
				buffer[1 + index],
				buffer[index]
			}), 0);
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransDouble(System.Byte[],System.Int32)" />
		public override double TransDouble(byte[] buffer, int index)
		{
			return BitConverter.ToDouble(ByteTransDataFormat8(new byte[8]
			{
				buffer[7 + index],
				buffer[6 + index],
				buffer[5 + index],
				buffer[4 + index],
				buffer[3 + index],
				buffer[2 + index],
				buffer[1 + index],
				buffer[index]
			}), 0);
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransByte(System.Int16[])" />
		public override byte[] TransByte(short[] values)
		{
			if (values == null)
			{
				return null;
			}
			byte[] array = new byte[values.Length * 2];
			for (int i = 0; i < values.Length; i++)
			{
				byte[] bytes = BitConverter.GetBytes(values[i]);
				Array.Reverse(bytes);
				bytes.CopyTo(array, 2 * i);
			}
			return array;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransByte(System.UInt16[])" />
		public override byte[] TransByte(ushort[] values)
		{
			if (values == null)
			{
				return null;
			}
			byte[] array = new byte[values.Length * 2];
			for (int i = 0; i < values.Length; i++)
			{
				byte[] bytes = BitConverter.GetBytes(values[i]);
				Array.Reverse(bytes);
				bytes.CopyTo(array, 2 * i);
			}
			return array;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransByte(System.Int32[])" />
		public override byte[] TransByte(int[] values)
		{
			if (values == null)
			{
				return null;
			}
			byte[] array = new byte[values.Length * 4];
			for (int i = 0; i < values.Length; i++)
			{
				byte[] bytes = BitConverter.GetBytes(values[i]);
				Array.Reverse(bytes);
				ByteTransDataFormat4(bytes).CopyTo(array, 4 * i);
			}
			return array;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransByte(System.UInt32[])" />
		public override byte[] TransByte(uint[] values)
		{
			if (values == null)
			{
				return null;
			}
			byte[] array = new byte[values.Length * 4];
			for (int i = 0; i < values.Length; i++)
			{
				byte[] bytes = BitConverter.GetBytes(values[i]);
				Array.Reverse(bytes);
				ByteTransDataFormat4(bytes).CopyTo(array, 4 * i);
			}
			return array;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransByte(System.Int64[])" />
		public override byte[] TransByte(long[] values)
		{
			if (values == null)
			{
				return null;
			}
			byte[] array = new byte[values.Length * 8];
			for (int i = 0; i < values.Length; i++)
			{
				byte[] bytes = BitConverter.GetBytes(values[i]);
				Array.Reverse(bytes);
				ByteTransDataFormat8(bytes).CopyTo(array, 8 * i);
			}
			return array;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransByte(System.UInt64[])" />
		public override byte[] TransByte(ulong[] values)
		{
			if (values == null)
			{
				return null;
			}
			byte[] array = new byte[values.Length * 8];
			for (int i = 0; i < values.Length; i++)
			{
				byte[] bytes = BitConverter.GetBytes(values[i]);
				Array.Reverse(bytes);
				ByteTransDataFormat8(bytes).CopyTo(array, 8 * i);
			}
			return array;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransByte(System.Single[])" />
		public override byte[] TransByte(float[] values)
		{
			if (values == null)
			{
				return null;
			}
			byte[] array = new byte[values.Length * 4];
			for (int i = 0; i < values.Length; i++)
			{
				byte[] bytes = BitConverter.GetBytes(values[i]);
				Array.Reverse(bytes);
				ByteTransDataFormat4(bytes).CopyTo(array, 4 * i);
			}
			return array;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IByteTransform.TransByte(System.Double[])" />
		/// <returns>buffer数据</returns>
		public override byte[] TransByte(double[] values)
		{
			if (values == null)
			{
				return null;
			}
			byte[] array = new byte[values.Length * 8];
			for (int i = 0; i < values.Length; i++)
			{
				byte[] bytes = BitConverter.GetBytes(values[i]);
				Array.Reverse(bytes);
				ByteTransDataFormat8(bytes).CopyTo(array, 8 * i);
			}
			return array;
		}

		/// <inheritdoc />
		public override string ToString()
		{
			return $"ReverseBytesTransform[{base.DataFormat}]";
		}
	}
}
